<?php

namespace api\behaviors\actions;

use Yii;
use yii\base\Model;
use yii\helpers\Url;
use yii\web\ServerErrorHttpException;


class DeleteAction extends \yii\rest\DeleteAction
{
    public $is_delete_column;
    public $is_delete_value = 1;

    /**
     * @param mixed $id
     * @return array|void
     * @throws ServerErrorHttpException
     * @throws \yii\web\NotFoundHttpException
     */
    public function run($id)
    {
        $model = $this->findModel($id);

        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $this->id, $model);
        }

        if(!$this->is_delete_column){
            throw new ServerErrorHttpException('缺少必要的is_delete_column参数');
        }

        // 使用注入的方式，修改删除字段及其值
        $model->{$this->is_delete_column} = $this->is_delete_value;

        if ($model->update() === false) {
            throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
        }

        // TODO statusCode 默认204
        Yii::$app->getResponse()->setStatusCode(200);
        return [
            'msg' => '删除成功',
            'code' => 200
        ];
    }

}
